/*
 * Copyright (c) 2011 Ball State University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package edu.bsu.android.gtv.apps.bconnected.data;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

import android.app.Activity;

import edu.bsu.android.gtv.apps.bconnected.objects.Tutor;
import edu.bsu.android.gtv.apps.bconnected.parser.ParserFactory;
import edu.bsu.android.gtv.apps.bconnected.parser.ServiceParser;
import edu.bsu.android.gtv.apps.bconnected.util.Constants;

/**
 * Data layer for the {@link Tutor} object
 * @author Kyle Parker, University Computing Services
 */
public class TutorData {
    private static ArrayList<Tutor> mTutors;

    /**
     * Loads an ArrayList<Tutor> from the service handler containing the list of tutors for a specific course based on the term
     * @param bsuID
     * @param discCode
     * @param courseNum
     * @param section
     * @param termID
     * @param activity - used by the {@link ParserFactory} to retrieve the demo_account shared preference - this will determine what
     * 					 data source to use for the service handler.
     */
    public static void initializeTutor(String bsuID, String discCode, String courseNum, String section, String termID, Activity activity) 
			throws UnsupportedEncodingException {
    	ArrayList<String> querystringParms = new ArrayList<String>(6);
    	querystringParms.add(Constants.QUERYSTRING_BSU_ID + URLEncoder.encode(bsuID, "UTF-8"));
		querystringParms.add(Constants.QUERYSTRING_DISC_CODE + discCode); 
		querystringParms.add(Constants.QUERYSTRING_COURSE_NUM + courseNum); 
		querystringParms.add(Constants.QUERYSTRING_SECTION + section); 
		querystringParms.add(Constants.QUERYSTRING_TERM_ID + termID); 
			
		ServiceParser parser = ParserFactory.getParser(Constants.SERVICE_TYPE_TUTOR, querystringParms, activity);
		mTutors = parser.parseTutor();
    }

    /**
     * Returns the integer value of the {@link Tutor} object size
     * @return mTutors() || 0
     */
    public static int getTutorCount() {
    	if (mTutors == null) {
    		return 0;
    	} else {
            return mTutors.size();
    	}
    }

    /**
     * Returns the ArrayList<Tutor>
     * @return mTutors
     */
    public static ArrayList<Tutor> getTutors() {
        return mTutors;
    }

    /**
     * Returns a {@link Tutor} object at the index specified
     * @param i - the index of the object to be returned
     * @return mTutors(i)
     */   
    public static Tutor getTutor(int i) {
        return mTutors.get(i);
    }
}
